package com.duowei.diandan.app;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.duowei.diandan.R;
import com.duowei.diandan.constant.Settings;
import com.duowei.diandan.event.CountdownEvent;
import com.duowei.diandan.event.ToastEvent;
import com.duowei.diandan.view.CustomFrameLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Locale;

public class FrontHelper {

    private Handler mHandler = new Handler();

    private Activity mActivity;
    private WindowManager mWindowManager;
    private CustomFrameLayout mWindowLayout;

    public FrontHelper(Activity activity) {
        mActivity = activity;
    }

    public void start() {
        mWindowManager = (WindowManager) mActivity.getSystemService(Context.WINDOW_SERVICE);
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        params.format = PixelFormat.RGBA_8888;

        params.type = WindowManager.LayoutParams.TYPE_APPLICATION;
        params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;

        LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mWindowLayout = (CustomFrameLayout) inflater.inflate(R.layout.layout_window, null);
        mWindowManager.addView(mWindowLayout, params);

        TextView countdownView = mWindowLayout.findViewById(R.id.countdownView);
        if (Settings.COUNTDOWN_ENABLED) {
            countdownView.setVisibility(View.VISIBLE);
        } else {
            countdownView.setVisibility(View.GONE);
        }

        EventBus.getDefault().register(this);
    }

    public void stop() {
        EventBus.getDefault().unregister(this);

        if (mWindowManager != null) {
            mWindowManager.removeView(mWindowLayout);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCountdownEvent(CountdownEvent e) {
        mHandler.post(() -> {
            if (mWindowLayout != null) {
                TextView countdownView = mWindowLayout.findViewById(R.id.countdownView);
                if (countdownView != null) {
                    countdownView.setText(String.format(Locale.CHINA, "%ds", e.second));
                }
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showToast(ToastEvent event) {
        TextView tv = new TextView(mActivity);
        tv.setText(event.toast);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        tv.setTextColor(Color.WHITE);
        tv.setGravity(Gravity.CENTER);
        tv.setBackgroundResource(R.drawable.bg_toast);

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
        tv.setLayoutParams(params);
        mWindowLayout.addView(tv);

        AnimationSet as;
        if (event.animated) {
            as = (AnimationSet) AnimationUtils.loadAnimation(mActivity, R.anim.toast_animated_1);
        } else {
            as = (AnimationSet) AnimationUtils.loadAnimation(mActivity, R.anim.toast_animated_2);
        }
        as.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mHandler.post(() -> {
                    try {
                        mWindowLayout.removeView(tv);
                    } catch (Exception e) {
                        Toast.makeText(mActivity, "FrontHelper: " + e.toString(), Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        tv.setAnimation(as);
    }
}
